package jwdt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jwdt.xadmin.info.bean.TNews;

import com.ezerg.db.Jdb;
import com.ezerg.util.StringUtil;
import com.ezerg.web.Form;
import com.ezerg.web.engine.DefaultHtmlProvider;

public class OAHtmlProvider extends DefaultHtmlProvider {
	
	@Override
	protected String generateKey(String uri, Form form) {
		// 
		String key = form.getString("id");
		return StringUtil.replace(uri, "Detail.html", "-"+ key);
	}

	@Override
	protected boolean beforeExport(Form form, HttpServletRequest request, HttpServletResponse response) {
		// 过滤 post 请求
		String method = request.getMethod();
		if("post".equals(method)) {
			return false;
		}
		// 只更新详情页
		String path = request.getServletPath();
		if(path.startsWith("/detail")) {
			int id = form.getInt("id");
			if(id != 0) {//信息
				TNews news = (TNews)Jdb.get().get(TNews.class, id);
				if(StringUtil.nullOrBlank(news.getUrl())) {
					return true;
				}
			}
		}
		// 
		return false;
	}

	@Override
	protected void afterExport(String fileName, Form form) {
		String url = StringUtil.replace(fileName, getRealPath(), "");
		// 
		int id = form.getInt("id");
		if(id != 0) {//信息
			TNews news = (TNews)Jdb.get().get(TNews.class, id);
			news.setUrl(url);
			try {
				Jdb.get().update(news);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
